﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.IO;

namespace DisplayBoardInfo.Server.Forms.Board
{
    /// <summary>
    /// Interaction logic for Advertisement.xaml
    /// </summary>
    public partial class Advertisement : UserControl
    {
        //Timer to change clock
        private DispatcherTimer timer;
        FileInfo[] rgFiles;
        int i = 0;

        public Advertisement()
        {
            InitializeComponent();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (rgFiles != null)
            {
                if (rgFiles.Count() > 0)
                {
                    if (rgFiles.Count() == ++i) i = 0;
                    BitmapImage bi3 = new BitmapImage();
                    bi3.BeginInit();
                    bi3.UriSource = new Uri(rgFiles[i].FullName, UriKind.RelativeOrAbsolute);
                    bi3.EndInit();
                    reklama.Source = bi3;
                }
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo("reklamy");
                rgFiles = di.GetFiles("*.bmp");
            }
            catch (Exception)
            {
                //Nie pokazuj, że coś jest źle
                this.Visibility = System.Windows.Visibility.Hidden;
            }

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }
    }
}
